<template>
  <div style="margin: 15px">
    <Form :model="formData" style="width: 500px" :label-width="80">
      <FormItem label="登录名">
        <Input v-model="formData.loginName" />
      </FormItem>
      <FormItem label="所属机构">
        <Input v-model="formData.orgName" disabled />
      </FormItem>
      <FormItem label="联系电话">
        <Input v-model="formData.mobile" />
      </FormItem>
      <FormItem label="真实姓名">
        <Input v-model="formData.staffName" />
      </FormItem>
      <FormItem label="性别">
        <RadioGroup v-model="formData.sex">
          <Radio label="m">男</Radio>
          <Radio label="f">女</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="老师标签">
        <Select v-model="formData.sysLabels" multiple>
          <Option v-for="item in teacherTagList" :value="item.id" :key="item.id">{{ item.labelName }}</Option>
        </Select>
      </FormItem>
      <FormItem label="是否名师">
        <Select v-model="formData.topTeacher">
          <Option value="0">是</Option>
          <Option value="1">否</Option>
        </Select>
      </FormItem>
      <FormItem label="老师风采">
        <div class="demo-upload-list" v-for="item in uploadList">
          <template v-if="item.status === 'finished'">
            <img :src="item.url">
            <div class="demo-upload-list-cover">
              <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
              <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
            </div>
          </template>
          <template v-else>
            <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
          </template>
        </div>
        <Upload
          ref="upload"
          :show-upload-list="false"
          :on-success="handleSuccess"
          :format="['jpg','jpeg','png']"
          :before-upload="handleBeforeUpload"
          :headers="headers"
          multiple
          type="drag"
          accept="image/*"
          :action="url"
          style="display: inline-block;width:58px;">
          <div style="width: 58px;height:58px;line-height: 58px;">
            <Icon type="ios-camera" size="20"></Icon>
          </div>
        </Upload>
        <Modal title="查看大图" v-model="visible">
          <img :src="imgName" v-if="visible" style="width: 100%">
        </Modal>
      </FormItem>
      <Button @click="submit" style="margin-left: 40px;width: 150px" type="warning">修改</Button>
    </Form>

  </div>
</template>

<script>
  export default {
    name: 'teacher-data',
    data() {
      return {
        headers: {'x-token': localStorage.getItem('x-token')},
        url: '/api/api/picture/upload',
        formData: {},
        teacherTagList: [],
        pic: [],
        imgName: '',
        visible: false,
        uploadList: []
      }
    },
    mounted() {
      this.detail()
      this.teacherTag()
    },
    methods: {
      teacherTag() {
        this.$get('/api/api/admin/label/list', {labelCategory: '1'}).then((res) => {
          if (res.code === 0) {
            this.teacherTagList = res.data
          }
        })
      },
      detail() {
        this.$get('/api/api/org/teacher/detail').then((res) => {
          if (res.code === 0) {
            this.formData = res.data
            if (res.data.photo) {
              this.pic = res.data.photo
              this.uploadList = [{status: 'finished', url: res.data.photo}]
            }
          }
        })
      },
      submit() {
        this.$Modal.confirm({
          title: '你确定要修改吗？',
          okText: '确定',
          cancelText: '取消',
          onOk: () => {
            this.$put('/api/api/org/teacher/editOne', {
              id: this.formData.id,
              loginName: this.formData.loginName,
              mobile: this.formData.mobile,
              staffName: this.formData.staffName,
              sex: this.formData.sex,
              sysLabel: this.formData.sysLabels,
              topTeacher: this.formData.topTeacher,
              photo: this.pic[0]
            }).then((res) => {
              if (res.code === 0) {
                if (res.data.code === 20004) {
                  this.$Message.warning('账户名重复')
                  return false
                }
                this.$Message.success('修改成功')
                this.detail()
              } else {
                this.$Message.error('修改成功')
              }
            }).catch((res) => {
              this.$Message.error('修改成功')
            })
          }
        })
      },
      handleView (url) {
        this.imgName = url
        this.visible = true
      },
      handleRemove (file) {
        const fileList = this.$refs.upload.fileList
        this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
        let url = '/api/api/picture/delete'
        let fileUrl = {
          fileUrl: file.url
        }
        this.$deletes(url, fileUrl).then((res) => {
          if (res.message) {
            this.pic.splice(this.pic.indexOf(file), 1)
            this.uploadList.splice(this.uploadList.indexOf(file), 1)
            this.$Message.success('删除成功')
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      handleSuccess (res, file) {
        file.url = file.response.data
        this.$Message.success('上传成功')
        console.log(this.pic)
        console.log(this.uploadList)
        this.pic.push(file.response.data)
        this.uploadList.push(file)
      },
      handleBeforeUpload () {
        const check = this.uploadList.length < 1
        if (!check) {
          this.$Message.error('最多上传1张图片')
        }
        return check
      }
    }
  }
</script>

<style scoped>
  .demo-upload-list{
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list img{
    width: 100%;
    height: 100%;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>
